/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.Connect;
import data.Constant;
import data.User_Delete;
import data.List_User;
import data.UserInfo;
/**
 *
 * @author NS
 */
@WebServlet("/Delete")
public class Delete extends HttpServlet {
    
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */


    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        //Get parameter from UserInfo.jsp
//        String delUser = request.getParameter("delUser");
        String delUser     = request.getParameter("del");
        System.out.println("delUser = " + delUser);
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
//        //Set user information
//        UserInfo userinf = new UserInfo();
//        userinf.setID(Integer.parseInt(ID));
//        userinf.setUsername(username);
//        userinf.setFullname(fullname);
//        userinf.setAddr(addr);
                
        //System.out.println(delUser);
        
        
        Connect connection = new Connect();
        String connectData = connection.connectData(Constant.OracleAdminUsr,Constant.OracleAdminPwd);
        
        if(!connectData.equals("success")){
            out.println("<h1><font color = #FF0000>Cannot connect to database...</font></h1>");
            return;
        }
        
        User_Delete del = new User_Delete(connection.getConnection());
        UserInfo curUser = (UserInfo)session.getAttribute("user");
        del.delUser(delUser,curUser.getUsername());
        
        List_User u = new List_User(connection.getConnection());
        ArrayList<UserInfo> userList = u.queryUserList(curUser.getUsername());
        
        connection.closeConnection();
        
        
//        request.setAttribute("user", userinf);
//        request.setAttribute("userList", userList);
        //session.setAttribute("user", userinf);
        session.setAttribute("userList", userList);
        String url = "/UserInfo.jsp";
	RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
	dispatcher.forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
